package com.gqz.patrol.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.patrol.entity.EnWzSblxEntity;
import com.gqz.patrol.mapper.EnWzSblxMapper;
import com.gqz.patrol.mapstruct.EnWzSblxMapstruct;
import com.gqz.patrol.service.IEnWzSblxService;
import com.gqz.patrol.vo.EnWzSblxVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 新能源-物资-设备类型树 ҵ���߼�ʵ�ֲ�ServiceImpl
 */
@Service
@Slf4j
public class EnWzSblxServiceImpl extends ServiceImpl<EnWzSblxMapper, EnWzSblxEntity> implements IEnWzSblxService {

    @Qualifier("enWzSblxMapstructImpl")
    @Autowired
    private EnWzSblxMapstruct mapstruct;

    @Override
    public List<EnWzSblxVo> getById(String id) {
        MPJLambdaWrapper<EnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnWzSblxEntity::getId, id);
        queryWrapper.selectAll(EnWzSblxEntity.class);
        List<EnWzSblxEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnWzSblxVo> page(RequestForm<EnWzSblxVo> requestForm) {
        EnWzSblxEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzSblxEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnWzSblxEntity.class);
        Page<EnWzSblxEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnWzSblxEntity> iPage = baseMapper.selectJoinPage(page, EnWzSblxEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnWzSblxVo> requestForm) {
        EnWzSblxEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzSblxEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnWzSblxEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnWzSblxVo> forms) {
        List<EnWzSblxEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzSblxEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnWzSblxVo> forms) {
        List<EnWzSblxEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzSblxEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


    @Override
    public List<EnWzSblxVo> getList(EnWzSblxVo iEnWzSblxVo) {
        MPJLambdaWrapper<EnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(EnWzSblxEntity.class);
        if (iEnWzSblxVo.getIsLeaf() != null && !"".equals(iEnWzSblxVo.getIsLeaf()) && iEnWzSblxVo.getIsLeaf() != "") {
            queryWrapper.eq(EnWzSblxEntity::getIsLeaf, iEnWzSblxVo.getIsLeaf());
        }
        if (iEnWzSblxVo.getPid() != null && !"".equals(iEnWzSblxVo.getPid()) && iEnWzSblxVo.getPid() != "") {
            queryWrapper.eq(EnWzSblxEntity::getPid, iEnWzSblxVo.getPid());
        }
        queryWrapper.eq(EnWzSblxEntity::getSfsc, 0);
        queryWrapper.orderByAsc(EnWzSblxEntity::getOrderNo);
        List<EnWzSblxEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }


    @Override
    public List<EnWzSblxVo> queryLeaf(String pid) {
        MPJLambdaWrapper<EnWzSblxEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(EnWzSblxEntity.class);
        queryWrapper.eq(EnWzSblxEntity::getPid, pid);
        queryWrapper.eq(EnWzSblxEntity::getSfsc, 0);
        //queryWrapper.eq(TEnInterfaceCodeEntity::getValue, value);
        queryWrapper.orderByAsc(EnWzSblxEntity::getOrderNo);

        List<EnWzSblxEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }

}
